package com.dongdongshop.serviceimpl;

import com.dongdongshop.mapper.TbSellerMapper;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.pojo.TbSellerExample;
import com.dongdongshop.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class SellerServiceImpl implements SellerService {
    @Autowired
    private TbSellerMapper tbSellerMapper;
    //查询所有
    @Override
    public List<TbSeller> findAll(String name, String nickName) {
        TbSellerExample tse = new TbSellerExample();
        TbSellerExample.Criteria criteria = tse.createCriteria();
        if (name != null && !"".equals(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if (nickName != null && !"".equals(nickName)){
            criteria.andNickNameLike("%" + nickName + "%");
        }
        List<TbSeller> tbSellers = tbSellerMapper.selectByExample(tse);
        return tbSellers;
    }

    //查询当前商家对象
    @Override
    public TbSeller selectById(String sellerId) {
        TbSellerExample tse = new TbSellerExample();
        TbSellerExample.Criteria criteria = tse.createCriteria();
        criteria.andSellerIdEqualTo(sellerId);
        List<TbSeller> tbSellers = tbSellerMapper.selectByExample(tse);

        return tbSellers.get(0);
    }

    //修改商家状态
    @Override
    public Boolean updateStatus(TbSeller tabSeller) {
        try {
            int i = tbSellerMapper.updateByPrimaryKeySelective(tabSeller);
            if (i > 0){
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //商家注册
    @Override
    public Boolean insertSeller(TbSeller tbSeller) {
        int i = tbSellerMapper.insertSelective(tbSeller);
        if (i>0){
            return true;
        }
        return false;
    }

    //判断用户名
    @Override
    public TbSeller selectByTperson(String username) {
        TbSellerExample tse = new TbSellerExample();
        TbSellerExample.Criteria criteria = tse.createCriteria();
        criteria.andSellerIdEqualTo(username);
        List<TbSeller> tbSellers = tbSellerMapper.selectByExample(tse);
        return tbSellers.get(0);
    }

    @Override
    public TbSeller selectBySellerId(String sellerId) {
        TbSellerExample tse = new TbSellerExample();
        TbSellerExample.Criteria criteria = tse.createCriteria();
        criteria.andSellerIdEqualTo(sellerId);
        List<TbSeller> tbSellers = tbSellerMapper.selectByExample(tse);
        return tbSellers.get(0);
    }
}
